package io.dcloud.h527baaa6.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.dcloud.h527baaa6.R;
import io.dcloud.h527baaa6.bean.EventBigShotSayBean;
import io.dcloud.h527baaa6.bean.EventMediaBean;
import io.dcloud.h527baaa6.utils.PlayUtil;

/**
 * Date   2017/9/7
 * Editor  Misuzu
 * 首页语音
 */

public class HomeVoiceView extends LinearLayout {

    @BindView(R.id.bigshot_say_play)
    ImageView mBigshotSayPlay;
    @BindView(R.id.bigshot_say_progress)
    ProgressBar mBigshotSayProgress;
    private int flag=0;//表示当前播放的数据的条数
    private boolean isFirst=true;//表示是否是第一次点击播放按钮
    private boolean isPlaying=false;//表示按钮点击状态
    private String mUrl;
    PlayUtil utils;
    public HomeVoiceView(Context context) {
        super(context);
        initView();
    }

    public HomeVoiceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.view_bigshot_say_v3_layout, this);
        ButterKnife.bind(this, this);
        setOrientation(VERTICAL);
        utils = PlayUtil.getIns();

    }

    @OnClick({R.id.bigshot_say_play})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bigshot_say_play:
                if (isFirst){
                    utils.startNormalPlay(mUrl);
                    isFirst=false;
                    isPlaying=true;
                    mBigshotSayPlay.setImageResource(R.drawable.ic_zanting);
                }else {
                      if (isPlaying){
                          utils.onPause();//暂停
                          mBigshotSayPlay.setImageResource(R.drawable.ic_bofang);
                          isPlaying=false;
                      }else{
                          utils.onResum();//开始
                          mBigshotSayPlay.setImageResource(R.drawable.ic_zanting);
                          isPlaying=true;
                      }
                }
                break;
        }
    }
    /**
     * 还原状态
     */
    private void initType(){
        utils.stopPlay();
        isFirst=true;
        isPlaying=false;
        mBigshotSayPlay.setImageResource(R.drawable.ic_bofang);

    }
    /**
     * 设置数据
     */
    public void setData(String url){
        initType();
        mUrl=url;
        mBigshotSayProgress.setMax(100);
        mBigshotSayProgress.setProgress(0);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 进度更新
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onReceiveMessages(EventMediaBean bean) {
        if (!isPlaying)
            return;
        boolean finsh = bean.isFinsh();
        int curTime = bean.getCurTime();
        int totalTime = bean.getTotalTime();
        if (finsh){//播放结束了
            //完成自动播放功能
            utils.startNormalPlay(mUrl);
            isFirst=false;
            isPlaying=true;
            mBigshotSayPlay.setImageResource(R.drawable.ic_zanting);

        }else{//更新进度条状态
            mBigshotSayProgress.setProgress(getProgress(curTime,totalTime));
        }
    }
    /**
     * 接收离开界面的通知
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onReceiveMessages(EventBigShotSayBean bean) {
        String id2 = bean.getId();
       if ("close".equals(id2)) {//离开界面时,状态还原
          // initType();  修改 离开界面还原状态改为离开界面暂停播放
           utils.onPause();//暂停
           mBigshotSayPlay.setImageResource(R.drawable.ic_bofang);
           isPlaying=false;
        }
    }
    /**
     * 根据当前时间和总时间得到播放进度
     * @param curTime
     * @param totalTime
     * @return
     */
    private int getProgress(int curTime,int totalTime){
        if (curTime==0)
            return 0;
        return curTime*100/totalTime;
    }
}
